/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-04-25     Administrator       the first version
 */

#include "dev_time.h"
#include "stdio.h"
#include "string.h"
#include <sys/time.h>
#include <stdlib.h>
#include <ntp.h>
#include "user.h"

#include <rtthread.h>
#include <rtdevice.h>

#define RTC_NAME       "rtc"

char week_en[][arr_index_size] = {"111", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};

char mon_en[][arr_index_size] = {"111", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
        "Aug", "Sep", "Oct", "Nov", "Dec"};

int get_target_from_arr(char *tar, char src[][arr_index_size], int size)
{
    // printf("start =  %s : = %s \n", tar, strtok(src[0], "|"));
    strtok(src[0], "|");
    for (size_t i = 1; i < size; i++)
    {
        strtok(NULL, "|");
        // printf("%ld tar =  %s : = %s \n", i, tar, strtok(NULL, "|"));
        if (strcmp(tar, src[i]) == 0)
        {
            return i;
        }
    }
    return 0;
}


void sync_time_by_ntp(void)
{
    time_t cur_time = ntp_get_time("www.ntp.aliyun.com");
}



void set_dev_time(uint16_t year, uint8_t month, uint8_t day,uint16_t hour, uint8_t min, uint8_t sec)
{
    if ( set_date(year, month, day) != RT_EOK)
    {
        rt_kprintf("set RTC date failed\n");
    }
    if (set_time(hour, min, sec) != RT_EOK)
    {
        rt_kprintf("set RTC time failed\n");
    }
}

void rtc_time_init(void)
{
    rt_device_t device = RT_NULL;

    /*寻找设备*/
    device = rt_device_find(RTC_NAME);
    if (!device)
    {
        rt_kprintf("find %s failed!", RTC_NAME);
    }
    /*初始化RTC设备*/
    if(rt_device_open(device, 0) != RT_EOK)
    {
        rt_kprintf("open %s failed!", RTC_NAME);
    }
    /* 设置日期 */
    set_dev_time(2023,04,27,19,04,05);
}



